<!doctype html>
<title>Incognito test</title>
<div id="resultDiv"></div>
<script>
var result;
var called = false;
var leaked = Object.create(null);
["run", "Binding", "loadTypeSchema", "binding"].forEach(function(name) {
  Object.defineProperty(Object.prototype, name, {
    configurable: true,
    enumerable: false,
    set: function(func) {
      if(name === "binding") {
        Object.defineProperty(this, name, { value: func });
        if(func) {
          this.extension = function() {};
        }
        return;
      } else if(name === "loadTypeSchema") {
        if(func) {
          Object.defineProperty(this, name, {
            value: function() {
              return { js_module: "extension", functions: [] };
            }
          });
          delete Object.prototype[name];
        }
        return;
      }

      leaked[name] = func;
      if(name !== "Binding") {
        delete Object.prototype[name];
      }

      Object.defineProperty(this, name, {
        writable: true,
        configurable: true,
        enumerable: true,
        value: func
      });

      if(name === "Binding" && func.prototype) {
        var old = func.prototype.registerCustomHook;
        func.prototype.registerCustomHook = function(f) {
          if(f.length === 2) {
            var o = {};
            f({
              compiledApi: o,
              apiFunctions: {
                setHandleRequest: function() {},
                setUpdateArgumentsPreValidate: function() {}
              }
            }, "");
            result = o;
          }
          return old.apply(this, arguments);
        };

        var old2 = func.prototype.generate;
        func.prototype.generate = function() {
          if(this.apiFunctions_ && this.apiFunctions_.namespace === "extension") {
            return {};
          }
          return old2.apply(this, arguments);
        };

        this[name] = func;

        if(!called) {
          called = true;
          func.prototype.generate.call({
            runHooks_: function() {},
            schema_: {
              namespace: "webstore",
              properties: {
                "": { type: "", $ref: 1, value: [] }
              }
            }
          });
        }
      }
    }
  });
});

chrome.webstore.onDownloadProgress;

leaked.run("", "", "", function() {
  chrome.runtime.lastError;
}, []);

resultDiv.textContent = "inIncognitoContext = " + String(result.inIncognitoContext);
</script>
